/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.chart.bar;

import kz.kstu.ent.desktop.chart.Chart;
import kz.kstu.ent.desktop.chart.info.BarChartInfo;
import kz.kstu.ent.util.ColorUtil;
import kz.kstu.ent.util.NumberUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

import java.awt.*;

/**
 * Date: 23.09.12
 * Time: 19:30
 *
 * @author Oleg Anastassov
 */
public abstract class Bar extends Chart {

    private DefaultCategoryDataset dataset;

    protected Bar(BarChartInfo info, DefaultCategoryDataset dataset) {
        super(info);
        this.dataset = dataset;
    }

    public DefaultCategoryDataset getDataset() {
        return dataset;
    }

    public void setDataset(DefaultCategoryDataset dataset) {
        this.dataset = dataset;
    }

    @Override
    public JFreeChart build() {
        BarChartInfo info = (BarChartInfo) this.getChartInfo();
        JFreeChart chart = ChartFactory.createBarChart3D(
                info.getTitle(),
                info.getxAxisLabel(),
                info.getyAxisLabel(),
                this.getDataset(),
                info.getPlotOrientation(),
                info.isShowLegend(),
                info.isShowToolTips(),
                info.isShowUrls()
        );
        return setDefaults(chart);
    }

    private JFreeChart setDefaults(JFreeChart chart) {
        chart.setBackgroundPaint(ColorUtil.BACKGROUND_COLOR);

        CategoryPlot p = chart.getCategoryPlot();

        // empty dataset message settings
        p.setNoDataMessage(NO_DATA_MESSAGE);
        p.setNoDataMessagePaint(Color.BLACK);

        CategoryItemRenderer categoryItemRenderer = p.getRenderer();
        categoryItemRenderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator("{2}", NumberUtil.DECIMAL_FORMAT)
        );
        categoryItemRenderer.setBaseItemLabelsVisible(true);

        BarRenderer renderer = (BarRenderer) p.getRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setBarPainter(new StandardBarPainter());

        // use to print values inside the bars
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER,
                TextAnchor.CENTER)
        );

        // use to print values over the bars
//        renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(
//                ItemLabelAnchor.CENTER,
//                TextAnchor.CENTER
//        ));

        return chart;
    }
}
